<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-简易轮播图</title>
    <style>
        body{
            background: radial-gradient(#ccc, rgb(127, 25, 136))
        }
        
        *{
            user-select: none;
        }
        .container{
            width: 640px;
            height: 360px;
            margin: 100px auto;
            position:relative;
        }
        .container img{
            width: 100%;
            position:absolute;
            left: 0;
            top: 0;
        }

        .btn{
            width: 50px;
            height: 50px;
            background: white;
            position:absolute;
            top: 155px;
            border-radius: 50%;            
            display:flex;
            justify-content: center;
            align-items: center;
        }
        .left{
            left: 10px;
        }
        .right{
            right: 10px;
        }
    </style>
</head>
<body>
    <div id="root">
        
    </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        class App extends React.Component{
            state = {
                index: 0,
                imgs: [
                    './images/1.jpg',
                    './images/2.jpg',
                    './images/3.jpg',
                    './images/4.jpg',
                    './images/5.jpg',
                ]
            }

            render(){
                //解构赋值
                let {index, imgs} = this.state;
                return <div className="container">
                    <div className="img-list">
                        <div className="item"><img src={imgs[index]} alt="" /></div>
                    </div>
                    <div className="btn left" onClick={this.prev}>&lt;</div>
                    <div className="btn right" onClick={this.next}>&gt;</div>
                </div>
            }
            //切换下一张
            next = () => {
                //获取index
                let {index, imgs} = this.state;
                //判断
                if(index < imgs.length - 1){
                    index = index+1
                }else{
                    index = 0;
                }
                this.setState({
                    index
                })
            }

            prev = () => {
                let {index, imgs} = this.state;
                //判断
                if(index >= 1){
                    index = index - 1;
                }else{
                    index = imgs.length - 1;
                }
                //设置
                this.setState({
                    index
                })
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"));

    </script>
</body>
</html>